var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
const key = "hashName";
module.exports = {
    init() {
        return __awaiter(this, void 0, void 0, function* () {
            let res = yield Redis.getData(key);
            let hashName = res.data;
            return hashName;
        });
    },
    login(data, new_sid) {
        return __awaiter(this, void 0, void 0, function* () {
            let Redis = require("../tools/redis.ts");
            var token = data.roomId;
            let is_token = yield Redis.isKey("tokenList", token);
            if (!is_token) {
                return {
                    code: 400,
                    data: {},
                    msg: "token有误或不存在！"
                };
            }
            let old_sid = yield Redis.hGetValue("tokenList", token);
            if (old_sid) {
                yield Redis.hDel("sidList", old_sid);
                return {
                    code: 300,
                    data: {
                        old_sid
                    },
                    msg: "有新的用户登录,请刷新重试"
                };
            }
            yield Redis.hSet("sidList", new_sid, token);
            yield Redis.hSet("tokenList", token, new_sid);
            yield Redis.hSet(token, "sid", new_sid);
        });
    },
    delLoginInfo(sid) {
        return __awaiter(this, void 0, void 0, function* () {
            let token = yield Redis.hGetValue("sidList", sid);
            if (token) {
                yield Redis.hDel("sidList", sid);
                // await Redis.hDel("tokenList", token)
                yield Redis.hSet("tokenList", token, "");
                yield Redis.Del(token);
            }
        });
    }
};
